package studyandroid.android.study.gophern.studyandroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.samonxu.qrcode.demo.samonxu__MainActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;

import studyandroid.android.study.gophern.studyandroid.c14multimedia.StillImageActivity;
import studyandroid.android.study.gophern.studyandroid.ui.listview.TestListView1Activity;
import studyandroid.android.study.gophern.studyandroid.ui.titlebar.StudyTitleBarActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
	LinearLayout mnOpenBrowser;
	LinearLayout mnGoogleMap;
	LinearLayout mnPhoneCall;
	LinearLayout mnSendSMS;
	LinearLayout mnSendEmail;
	LinearLayout mnPlayMedia;
	LinearLayout mnInstallApk;
	LinearLayout mnUninstallApk;
	LinearLayout mnAccessContacter;
	LinearLayout mnLoadPhoto;
	LinearLayout mnCamera;
	LinearLayout mn_samonxu_MainActiviy;
	LinearLayout mnStudyUI;
	LinearLayout mnStudyTitleBar;

	EditText edLog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		test();
	}

	/**
	 * 初始化组件
	 */
	private void initView() {
		mnOpenBrowser = (LinearLayout) findViewById(R.id.mnOpenBrowser);
		mnGoogleMap = (LinearLayout) findViewById(R.id.mnGoogleMap);
		mnPhoneCall = (LinearLayout) findViewById(R.id.mnPhoneCall);
		mnSendSMS = (LinearLayout) findViewById(R.id.mnSendSMS);
		mnSendEmail = (LinearLayout) findViewById(R.id.mnSendEmail);
		mnPlayMedia = (LinearLayout) findViewById(R.id.mnPlayMedia);
		mnInstallApk = (LinearLayout) findViewById(R.id.mnInstallApk);
		mnUninstallApk = (LinearLayout) findViewById(R.id.mnUninstallApk);
		mnAccessContacter = (LinearLayout) findViewById(R.id.mnAccessContacter);
		mnLoadPhoto = (LinearLayout) findViewById(R.id.mnLoadPhoto);
		mnCamera = (LinearLayout) findViewById(R.id.mnCamera);
		mn_samonxu_MainActiviy = (LinearLayout) findViewById(R.id.mn_samonxu__MainActivity);
		mnStudyUI = (LinearLayout) findViewById(R.id.mnStudyUI);
		mnStudyTitleBar = (LinearLayout) findViewById(R.id.mnStudyTitleBar);

		edLog = (EditText) findViewById(R.id.edLog);

		//--

		mnOpenBrowser.setOnClickListener(this);
		mnGoogleMap.setOnClickListener(this);
		mnPhoneCall.setOnClickListener(this);
		mnSendSMS.setOnClickListener(this);
		mnSendEmail.setOnClickListener(this);
		mnPlayMedia.setOnClickListener(this);
		mnInstallApk.setOnClickListener(this);
		mnUninstallApk.setOnClickListener(this);
		mnAccessContacter.setOnClickListener(this);
		mnLoadPhoto.setOnClickListener(this);
		mnCamera.setOnClickListener(this);
		mn_samonxu_MainActiviy.setOnClickListener(this);
		mnStudyUI.setOnClickListener(this);
		mnStudyTitleBar.setOnClickListener(this);

		//--

		findViewById(R.id.mnShowDialog).setOnClickListener(this);
		findViewById(R.id.mnPhoneDialer).setOnClickListener(this);
	}

	boolean True = true;
	boolean False = false;

	void test() {
		String path = Environment.getExternalStorageDirectory().getPath();
		edLog.setText(path + "\n" + getApplication().getPackageName());
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
			case R.id.mnOpenBrowser: //1.调用浏览器
				if (True) {
					Uri uri = Uri.parse("http://gophern.info:8080");
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				}
				break;
			case R.id.mnGoogleMap: //2.使用Google地图定位
				//失败,因为需要手机支持Google Map API：<uses-library android:name="com.google.android.maps"/>
				if (False) {
					Intent intent = new Intent(Intent.ACTION_VIEW,
						Uri.parse("http://ditu.google.cn/maps?hl=zh&mrt=loc&q=31.1198723,121.1099877"));
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
					intent.setClassName(this, "com.google.android.maps.MapActivity");
					startActivity(intent);
				}
				break;
			case R.id.mnPhoneCall: //3.电话呼叫功能
				if (True) {
					//权限：<uses-permission android:name="android.permission.CALL_PHONE" />
					if (Build.VERSION.SDK_INT >= 23 &&
						ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
						ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
						return;
					}
					Uri uri = Uri.parse("tel:13928036946");
					Intent intent = new Intent(Intent.ACTION_CALL, uri);
					startActivity(intent);
				}
				break;
			case R.id.mnSendSMS: //4.发送SMS
				if (True) {
					//不需要权限
					Uri uri = Uri.parse("smsto:13928036946");
					Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
					intent.putExtra("sms_body", "4.发送SMS");
					startActivity(intent);
				}
				break;
			case R.id.mnSendEmail: //5.发送Email
				if (True) {
					//不需要权限
					Intent intent = new Intent(Intent.ACTION_SEND);
					String[] tos = {"gophern@qq.com"};
					String[] ccs = {"gophern@163.com", "gophern@wistracker.com"};
					intent.putExtra(Intent.EXTRA_EMAIL, tos);
					intent.putExtra(Intent.EXTRA_CC, ccs);
					intent.putExtra(Intent.EXTRA_TEXT, "来自<font color='red'>StudyAndroid</font>的邮件。");
					intent.putExtra(Intent.EXTRA_SUBJECT, "StudyAndroid邮件" + (new Date()).toString());
					intent.setType("message/rfc822");
					startActivity(Intent.createChooser(intent, "选择Email客户端工具"));
				}
				break;
			case R.id.mnPlayMedia: //6.播放多媒体文件
				if (True) {
					//不需要权限
					Intent intent = new Intent(Intent.ACTION_VIEW);
					Uri uri = Uri.parse("file:///sdcard/Download/时间流逝.mp3");
					intent.setDataAndType(uri, "audio/mp3");
					startActivity(intent);
				}
				break;
			case R.id.mnInstallApk: //7.安装APK文件
				if (True) {
					//不需要权限
					String path = Environment.getExternalStorageDirectory().getPath();
					String fileName = path + "/Download/StudyAndroid.apk";
					edLog.setText(fileName);
					Toast.makeText(this, fileName, Toast.LENGTH_SHORT);
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
					startActivity(intent);
				}
				break;
			case R.id.mnUninstallApk: //8.卸载应用程序
				if (True) {
					Uri uri = Uri.fromParts("package", getApplication().getPackageName(), null);
					Intent intent = new Intent(Intent.ACTION_DELETE, uri);
					startActivity(intent);
				}
				break;
			case R.id.mnAccessContacter: //9.访问联系人
				if (True) {
					Intent intent = new Intent();
					intent.setAction(Intent.ACTION_VIEW);
					intent.setData(ContactsContract.Contacts.CONTENT_URI);
					startActivity(intent);
				}
				break;
			case R.id.mnLoadPhoto: //10.从图库选择图片
				if (True) {
					Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
					intent.addCategory(Intent.CATEGORY_OPENABLE);
					intent.setType("image/*");
					intent.setData(ContactsContract.Contacts.CONTENT_URI);
					startActivityForResult(intent, 0);
				}
				break;

			case R.id.mnShowDialog:
				new AlertDialog.Builder(this).setTitle("确认退出吗？")
					.setIcon(android.R.drawable.ic_dialog_info)
					.setCancelable(false)
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 点击“确认”后的操作
							MainActivity.this.finish();

						}
					})
					.setNegativeButton("返回", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 点击“返回”后的操作,这里不设置没有任何操作
						}
					}).show();
				break;
			case R.id.mnPhoneDialer: //电话拨号盘
				if (True) {
					//权限：<uses-permission android:name="android.permission.CALL_PHONE" />
					if (Build.VERSION.SDK_INT >= 23 &&
						ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
						ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
						return;
					}
					Uri uri = Uri.parse("tel:13928036946");
					Intent intent = new Intent(Intent.ACTION_DIAL, uri);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					startActivity(intent);
				}
				break;
			case R.id.mnCamera: //摄像头
				if (True) {
					Intent intent = new Intent(MainActivity.this, StillImageActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.mn_samonxu__MainActivity: //扫描
				if (True) {
					Intent intent = new Intent(MainActivity.this, samonxu__MainActivity.class);
					startActivity(intent);
				}
				break;
			case R.id.mnStudyUI:    //研究UI
				if (True) {
					Intent intent = new Intent(MainActivity.this, TestListView1Activity.class);
					startActivity(intent);
				}
				break;
		case R.id.mnStudyTitleBar:    //标题栏
				if (True) {
//					Intent intent = new Intent(MainActivity.this, MyTitleBarActivity.class);
					Intent intent = new Intent(MainActivity.this, StudyTitleBarActivity.class);
					startActivity(intent);
				}
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK && requestCode == 0) {
			Uri uri = data.getData();
			//显示所选图片的URI路径
			Toast.makeText(this, uri.getPath(), Toast.LENGTH_LONG).show();

			//显示图片
			Log.e("uri", uri.toString());
			ContentResolver cr = this.getContentResolver();
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
				ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
				imageView1.setImageBitmap(bitmap);
			} catch (FileNotFoundException e) {
				Log.e("Exception", e.getMessage(), e);
			}
		}
	}
}
